package com.zhaoqi.hr.entity;

import javax.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 工作合同实体
 */
@Entity
@Table(name = "work_contracts")
@EntityListeners(AuditingEntityListener.class)
public class WorkContract {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(unique = true, nullable = false, length = 50)
    private String contractNumber; // 合同编号
    
    @Column(name = "job_application_id", nullable = false)
    private Long jobApplicationId; // 岗位申请ID
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employer_id", nullable = false)
    private EmployerProfile employer; // 雇主
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "worker_id", nullable = false)
    private WorkerProfile worker; // 劳动者
    
    @Column(nullable = false, length = 200)
    private String contractTitle; // 合同标题
    
    @Column(columnDefinition = "TEXT")
    private String workDescription; // 工作内容描述
    
    @Column(length = 200)
    private String workLocation; // 工作地点
    
    @Column
    private LocalDateTime startTime; // 开始时间
    
    @Column
    private LocalDateTime endTime; // 结束时间
    
    @Column(precision = 8, scale = 2)
    private BigDecimal salary; // 约定薪资
    
    @Column(length = 50)
    private String salaryType; // 薪资类型
    
    @Column(length = 100)
    private String paymentMethod; // 付款方式
    
    @Column(columnDefinition = "TEXT")
    private String otherTerms; // 其他条款
    
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private ContractStatus status = ContractStatus.DRAFT;
    
    @Column
    private Boolean employerSigned = false; // 雇主签署状态
    
    @Column
    private LocalDateTime employerSignTime; // 雇主签署时间
    
    @Column
    private Boolean workerSigned = false; // 劳动者签署状态
    
    @Column
    private LocalDateTime workerSignTime; // 劳动者签署时间
    
    @Column(length = 100)
    private String thirdPartyContractId; // 第三方合同ID（法大大等）
    
    @Column(length = 500)
    private String contractFileUrl; // 电子合同文件URL
    
    @Column(columnDefinition = "TEXT")
    private String remark; // 备注
    
    @Column(columnDefinition = "TEXT")
    private String terminationReason; // 终止原因
    
    @Column
    private LocalDateTime terminatedAt; // 终止时间
    
    @CreatedDate
    @Column(nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(nullable = false)
    private LocalDateTime updatedAt;
    
    // 关联关系
    @OneToMany(mappedBy = "contract", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<WorkRecord> workRecords;
    
    @OneToMany(mappedBy = "contract", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Settlement> settlements;

    // Getter and Setter methods
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContractNumber() {
        return contractNumber;
    }

    public void setContractNumber(String contractNumber) {
        this.contractNumber = contractNumber;
    }

    public Long getJobApplicationId() {
        return jobApplicationId;
    }

    public void setJobApplicationId(Long jobApplicationId) {
        this.jobApplicationId = jobApplicationId;
    }

    public EmployerProfile getEmployer() {
        return employer;
    }

    public void setEmployer(EmployerProfile employer) {
        this.employer = employer;
    }

    public WorkerProfile getWorker() {
        return worker;
    }

    public void setWorker(WorkerProfile worker) {
        this.worker = worker;
    }

    public String getContractTitle() {
        return contractTitle;
    }

    public void setContractTitle(String contractTitle) {
        this.contractTitle = contractTitle;
    }

    public String getWorkDescription() {
        return workDescription;
    }

    public void setWorkDescription(String workDescription) {
        this.workDescription = workDescription;
    }

    public String getWorkLocation() {
        return workLocation;
    }

    public void setWorkLocation(String workLocation) {
        this.workLocation = workLocation;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public LocalDateTime getEndTime() {
        return endTime;
    }

    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
    }

    public BigDecimal getSalary() {
        return salary;
    }

    public void setSalary(BigDecimal salary) {
        this.salary = salary;
    }

    public String getSalaryType() {
        return salaryType;
    }

    public void setSalaryType(String salaryType) {
        this.salaryType = salaryType;
    }

    public String getPaymentMethod() {
        return paymentMethod;
    }

    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }

    public String getOtherTerms() {
        return otherTerms;
    }

    public void setOtherTerms(String otherTerms) {
        this.otherTerms = otherTerms;
    }

    public ContractStatus getStatus() {
        return status;
    }

    public void setStatus(ContractStatus status) {
        this.status = status;
    }

    public Boolean getEmployerSigned() {
        return employerSigned;
    }

    public void setEmployerSigned(Boolean employerSigned) {
        this.employerSigned = employerSigned;
    }

    public LocalDateTime getEmployerSignTime() {
        return employerSignTime;
    }

    public void setEmployerSignTime(LocalDateTime employerSignTime) {
        this.employerSignTime = employerSignTime;
    }

    public Boolean getWorkerSigned() {
        return workerSigned;
    }

    public void setWorkerSigned(Boolean workerSigned) {
        this.workerSigned = workerSigned;
    }

    public LocalDateTime getWorkerSignTime() {
        return workerSignTime;
    }

    public void setWorkerSignTime(LocalDateTime workerSignTime) {
        this.workerSignTime = workerSignTime;
    }

    public String getThirdPartyContractId() {
        return thirdPartyContractId;
    }

    public void setThirdPartyContractId(String thirdPartyContractId) {
        this.thirdPartyContractId = thirdPartyContractId;
    }

    public String getContractFileUrl() {
        return contractFileUrl;
    }

    public void setContractFileUrl(String contractFileUrl) {
        this.contractFileUrl = contractFileUrl;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getTerminationReason() {
        return terminationReason;
    }

    public void setTerminationReason(String terminationReason) {
        this.terminationReason = terminationReason;
    }

    public LocalDateTime getTerminatedAt() {
        return terminatedAt;
    }

    public void setTerminatedAt(LocalDateTime terminatedAt) {
        this.terminatedAt = terminatedAt;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public List<WorkRecord> getWorkRecords() {
        return workRecords;
    }

    public void setWorkRecords(List<WorkRecord> workRecords) {
        this.workRecords = workRecords;
    }

    public List<Settlement> getSettlements() {
        return settlements;
    }

    public void setSettlements(List<Settlement> settlements) {
        this.settlements = settlements;
    }
    
    /**
     * 合同状态枚举
     */
    public enum ContractStatus {
        DRAFT("草稿"),
        PENDING_SIGN("待签署"),
        SIGNED("已签署"),
        ACTIVE("履行中"),
        COMPLETED("已完成"),
        CANCELLED("已取消"),
        TERMINATED("已终止");
        
        private final String description;
        
        ContractStatus(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
}
